package me.keiwu.user.controller;

import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.user.domain.CommentDetail;
import me.keiwu.user.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by kei on 02/02/2018.
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;


    @RequestMapping("list")
    public ResponseV2 commentList(HttpServletRequest request,
                                  @RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size) {
        Long owner = ContextUtil.getLoginUserId(request);
        return ResponseV2.ok(commentService.list(owner, page, size));
    }


    @RequestMapping("detail")
    public ResponseV2 reply(Long commentId,
                            @RequestParam(defaultValue = "0") Integer page,
                            @RequestParam(defaultValue = "10") Integer size) {
        return ResponseV2.ok(commentService.detail(commentId, page, size));
    }

    @RequestMapping("comment")
    public ResponseV2 comment(HttpServletRequest request, Long owner, String message) {
        Long commenter = ContextUtil.getLoginUserId(request);
        return commentService.comment(owner, commenter, message);
    }


    @RequestMapping("get")
    public ResponseV2 get(HttpServletRequest request, Long owner) {
        Long commenter = ContextUtil.getLoginUserId(request);
        return ResponseV2.ok(commentService.getComment(owner, commenter));
    }

//    @RequestMapping("testComment")
//    public ResponseV2 testComment(HttpServletRequest request) throws UnsupportedEncodingException {
//        return commentService.testComment((long)433, (long)11);
//    }
}
